package com.qfedu.mtl.controller;

import com.alibaba.fastjson.JSON;
import com.qfedu.mtl.dto.BasicDto;
import com.qfedu.mtl.entity.BasicInfo;
import com.qfedu.mtl.entity.InfoDetail;
import com.qfedu.mtl.query.BasicQuery;
import com.qfedu.mtl.query.SelectQuery;
import com.qfedu.mtl.service.BasiService;
import com.qfedu.mtl.service.impl.BasiServiceImpl;
import com.qfedu.mtl.util.CharacterUtil;
import com.qfedu.mtl.util.HttpUtil;
import com.qfedu.mtl.util.StrUtil;
import com.qfedu.mtl.util.page.Pager;
import com.qfedu.mtl.util.web.annotation.Controller;
import com.qfedu.mtl.util.web.annotation.RequestMapping;
import com.qfedu.mtl.vo.TableVO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Controller
@RequestMapping("/basic")
public class BasicController {
    //引入业务层
    private BasiService service = new BasiServiceImpl();

    //查询对应类目id的全部选项
    @RequestMapping("/list3.do")
    public void list3(HttpServletRequest request,HttpServletResponse response) throws IOException {
        CharacterUtil.setContentWithJson(request, response);
        //获得类目的id
//        String ids = request.getParameter("gid");
        String ids = StrUtil.ifNull(request.getParameter("gid"),"1");
        int id = Integer.valueOf(ids);
        //分页参数
        String page = StrUtil.ifNull(request.getParameter("page"),"1");
        String size = StrUtil.ifNull(request.getParameter("size"),"10");
        //查询条件
        SelectQuery query = HttpUtil.request2Bean(request, SelectQuery.class);
        //调用业务方法分页
        Pager<InfoDetail> pager = service.findPage3(id,Integer.valueOf(page), Integer.valueOf(size), query);
        TableVO vo = TableVO.me().setData(pager.getRows()).setCount(pager.getTotal());
        //把VO转json
        String json = JSON.toJSONString(vo);
        response.getWriter().write(json);
    }

    //分页，对选项
    @RequestMapping("/list2.do")
    public void list2(HttpServletRequest request,HttpServletResponse response) throws IOException {
        CharacterUtil.setContentWithJson(request, response);
        //分页参数
        String page = StrUtil.ifNull(request.getParameter("page"),"1");
        String size = StrUtil.ifNull(request.getParameter("size"),"10");
        //查询条件
        SelectQuery query = HttpUtil.request2Bean(request, SelectQuery.class);
        //调用业务方法分页
        Pager<InfoDetail> pager = service.findPage2(Integer.valueOf(page), Integer.valueOf(size), query);
        // 前端 table - > {code:xxx  msg:xxx cont:xxx  data:xxx   }
        TableVO vo = TableVO.me().setData(pager.getRows()).setCount(pager.getTotal());
        //把VO转json
        String json = JSON.toJSONString(vo);
        response.getWriter().write(json);
    }


    //分页,对类目
    @RequestMapping("/list.do")
    public void list(HttpServletRequest request,HttpServletResponse response) throws IOException {
        CharacterUtil.setContentWithJson(request, response);
        //分页参数
        String page = StrUtil.ifNull(request.getParameter("page"),"1");
        String size = StrUtil.ifNull(request.getParameter("size"),"10");
        //查询条件
        BasicQuery query = HttpUtil.request2Bean(request, BasicQuery.class);
        System.out.println(query.toString());
        //调用业务方法分页
        Pager<BasicInfo> pager = service.findPage(Integer.valueOf(page), Integer.valueOf(size), query);
        TableVO vo = TableVO.me().setData(pager.getRows()).setCount(pager.getTotal());
        //把VO转json
        String json = JSON.toJSONString(vo);
        response.getWriter().write(json);
    }


    //添加评估类名
    @RequestMapping("/add.do")
    public void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CharacterUtil.setContentWithJson(request, response);
        BasicDto dto = HttpUtil.request2Bean(request, BasicDto.class);
        try{
            int row = service.add(dto);
            HttpUtil.responseSuccess(row,response);
        }catch (Exception e){
            HttpUtil.response(null,-1,e.getMessage(),response);
        }
    }

    //删除
    @RequestMapping("/delete.do")
    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CharacterUtil.setContentWithJson(request, response);
        String id = request.getParameter("id");
        if (StrUtil.isNullOrEmpty(id)) {
            HttpUtil.response(null,-1,"删除失败",response);
            return;
        }
        try{
            int row = service.delete(Integer.valueOf(id));
            HttpUtil.responseSuccess(null,response);
        }catch (Exception e){
            HttpUtil.response(null,-1,e.getMessage(),response);
        }
    }

    //批量删除
    @RequestMapping("/delBatch.do")
    public void delBatch(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //设置编码
        CharacterUtil.setContentWithJson(request, response);
        //获得前端批量删除IDS
        String ids = request.getParameter("ids");// ids =  ""
        //如果为null 停止删除
        if(StrUtil.isNullOrEmpty(ids) ){
            HttpUtil.response(null,-1,"请指定删除的id集合",response);
            return;
        }
        //执行删除
        String[] idss = ids.split(",");
        //转整型数组
        List<Integer> idsss = Stream.of(idss).map(e -> Integer.valueOf(e)).collect(Collectors.toList());
        try{
            service.deleteBatch(idsss);//批量删除
            HttpUtil.responseSuccess(null,response);
        }catch (Exception ex){
            HttpUtil.response(null,-1,ex.getMessage(),response);
        }
    }

    //查找一个通过id
    @RequestMapping("/select.do")
    public void select(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CharacterUtil.setContentWithJson(request, response);
        String id = request.getParameter("id");
        try{
            BasicInfo basic = service.select(Integer.valueOf(id));
            HttpUtil.responseSuccess(basic,response);
        }catch (Exception ex){
            HttpUtil.response(null,-1,ex.getMessage(),response);
        }
    }

    //查找所有的类目表数据
    @RequestMapping("/selectAll.do")
    public void selectAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CharacterUtil.setContentWithJson(request, response);
        try{
            List<BasicInfo> basics = service.selectAll();
            HttpUtil.responseSuccess(basics,response);
        }catch (Exception ex){
            HttpUtil.response(null,-1,ex.getMessage(),response);
        }
    }


    //修改类目信息
    @RequestMapping("/update.do")
    public void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CharacterUtil.setContentWithJson(request, response);
        BasicDto dto = HttpUtil.request2Bean(request, BasicDto.class);//获取不到数值类型的值,如果BasicDto中是int则获取不到，如果是Integer就获取得到
//        String id = request.getParameter("basic_info_id");
//        String name = request.getParameter("basic_info_name");
//        String status = request.getParameter("basic_info_status");
//        BasicDto dto = new BasicDto(Integer.valueOf(id),name,Integer.valueOf(status));
//        System.out.println(dto);//输出测试一下
        try{
            service.update(dto);
            HttpUtil.responseSuccess(null,response);
        }catch (Exception ex){
            HttpUtil.response(null,-1,ex.getMessage(),response);
        }
    }






    //根据类目的id查找对应的选项
    @RequestMapping("/findDetailByBasicId.do")
    public void findDetail(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CharacterUtil.setContentWithJson(request, response);
        String id = request.getParameter("id");
        System.out.println("id:" + id);
        try{
            List<InfoDetail> infoDetail = service.findDetail(Integer.valueOf(id));
            HttpUtil.responseSuccess(infoDetail,response);
        }catch (Exception ex){
            HttpUtil.response(null,-1,ex.getMessage(),response);
        }
    }

    //添加评估选项，提供类目的id,选项的名称，描述。。可选
    @RequestMapping("/addDetail.do")
    public void addDetail(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CharacterUtil.setContentWithJson(request, response);
        InfoDetail infoDetail = HttpUtil.request2Bean(request, InfoDetail.class);
        try{
            service.addDetail(infoDetail);
            HttpUtil.responseSuccess(null,response);
        }catch (Exception ex){
            HttpUtil.response(null,-1,ex.getMessage(),response);
        }
    }


    //删除不了，有外键约束
    @RequestMapping("/deleteDetail.do")
    public void deleteDetail(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CharacterUtil.setContentWithJson(request, response);
        String id = request.getParameter("id");
        try{
            service.deleteDetail(Integer.valueOf(id));
            HttpUtil.responseSuccess(null,response);
        }catch (Exception ex){
            HttpUtil.response(null,-1,ex.getMessage(),response);
        }
    }
    //批量删除
    @RequestMapping("/delBatch2.do")
    public void delBatch2(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //设置编码
        CharacterUtil.setContentWithJson(request, response);
        //获得前端批量删除ids,用逗号分割的字符串
        String ids = request.getParameter("ids");// ids =  "1,2,4,5"
        //如果为null 停止删除
        if(StrUtil.isNullOrEmpty(ids) ){
            HttpUtil.response(null,-1,"请指定删除的id集合",response);
            return;
        }
        //执行删除
        String[] idss = ids.split(",");
        //转整型数组
        List<Integer> idsss = Stream.of(idss).map(e -> Integer.valueOf(e)).collect(Collectors.toList());
        try{
            service.deleteBatch2(idsss);//批量删除
            HttpUtil.responseSuccess(null,response);
        }catch (Exception ex){
            HttpUtil.response(null,-1,ex.getMessage(),response);
        }
    }


    //修改评估选项 ，传入实体类
    @RequestMapping("/updateDetail.do")
    public void updateDetail(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CharacterUtil.setContentWithJson(request, response);
        //这里需要的参数要是，InfoDetail这个实体类对应的属性，不是数据库表中的字段
        InfoDetail infoDetail = HttpUtil.request2Bean(request, InfoDetail.class);
        try{
            service.updateDetail(infoDetail);
            HttpUtil.responseSuccess(null,response);
        }catch (Exception ex){
            HttpUtil.response(null,-1,ex.getMessage(),response);
        }
    }


    //提供商品的id、选项的id字符串。返回价格
    @RequestMapping("/getTotal.do")
    public void price(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CharacterUtil.setContentWithJson(request, response);
        //得到商品的id：参数为 good_id
        String good_id = request.getParameter("good_id");
        int g_id = Integer.valueOf(good_id);
        //得到选项的集合，约定是逗号分割的字符串如1,2,3,5 ,参数为detail_id
        String detail_ids = request.getParameter("detail_id");
        //执行业务，得到价格
        try{
            int total = service.getTotal(g_id,detail_ids);
            HttpUtil.responseSuccess(total,response);
        }catch (Exception ex){
            HttpUtil.response(null,-1,ex.getMessage(),response);
        }
    }


}
